﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace Movies
{
    public partial class FormDisplayMoviesInList : Form
    {
        
        public FormDisplayMoviesInList()
        {
            InitializeComponent();
        }

        private void MovieList_Load(object sender, EventArgs e)
        {
            backgroundWorker.RunWorkerAsync();            
        }
        
        private void MovieDetailsGrid_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row = MovieDetailsGrid.CurrentRow;
            //row.Selected = true;
            MovieDetails details = (MovieDetails)row.Tag;
            if (details != null)
            {
                FormDisplayMovieDetails disDetails = new FormDisplayMovieDetails(details);
                disDetails.ShowDialog();
            }
        }
        


        private void MovieDetailsGrid_CellClick(object sender, DataGridViewCellEventArgs e)
        {


        }

        private void initiateMovieLoad()
        {
            
            List<MovieDetails> movieDetails = HelperDataProvider.getDataStore().getMovieDefinitions();

            if (!(movieDetails != null && movieDetails.Count > 0) && !HelperDataProvider.getDataStore().isMoviesDefined())
            {
                if (!WinDisplayControllor.IsShownDone)
                {
                    Form childForm = new FormDefineMovieLocation();
                    WinDisplayControllor.IsShownDone = true;
                    childForm.ShowDialog();
                }
                else
                {
                    WinDisplayControllor.DisplayDialog("Use Movies menu option to define folder containing Movie(s)");
                }
                statusText.Text = "Movies not found";

                return;
            }

            int slNo = 0;
            int countTotal = 0;
            if (movieDetails != null && movieDetails.Count > 0)
            {
                countTotal = movieDetails.Count;
                progressBar.Maximum = countTotal;
                progressBar.Value = 0;
                progressBar.Show();
            
                foreach (MovieDetails detailsObj in movieDetails)
                {
                    if (!HelperDataProvider.getDataStore().isValidOnFilter(detailsObj))
                    {
                        progressBar.Value += 1;
                        continue;
                    }

                    slNo++;

                    String[] details = detailsObj.GetMovieDetailsAsArray();
                    
                    int index = MovieDetailsGrid.Rows.Add(details);
                    MovieDetailsGrid.Rows.SharedRow(index).Tag = detailsObj;
                    progressBar.Value += 1;
                }
                progressBar.Hide();
                
                if (HelperDataProvider.getDataStore().isFilterDefined())
                {
                    clearFilter.Show();
                    applyFilterButton.Text = "Modify Filter";
                }
                applyFilterButton.Show();
            }
            
            statusText.Text = getStatus(slNo, countTotal);
        }

        private String getStatus(int count, int countTotal)
        {
            String status = "";
            if (countTotal == 0)
            {
                status = "Movie(s) not found to display";
            }
            else if (count == 0 && count < countTotal)
            {
                status = "Showing None in " + countTotal + (countTotal > 1 ? " Items" : " Item");
            }
            else if (countTotal == 1 && count == 1)
            {
                status = "Showing Available " + count + " Item";
            }
            else if (count > 1 && count == countTotal)
            {
                status = "Showing All " + count + " Items";
            }
            else if (count > 0 && count < countTotal)
            {
                status = "Showing " + count + " of " + countTotal + (countTotal > 1 ? " Items" : " Item");
            }
            return status;
        }

        private String[] appendAtStart(String[] source, String append)
        {
            String[] newStr;
            if (source != null)
            {
                newStr = new String[source.Length + 1];
                for (int index = 0; index < source.Length; index++)
                {
                    newStr[index + 1] = source[index];
                }
            }
            else
            {
                newStr = new String[1];
            }
            newStr[0] = append;

            return newStr;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Dispose();
            WinDisplayControllor.ShowMovieShelve();
        }

        private void applyFilterButton_Click(object sender, EventArgs e)
        {
            WinDisplayControllor.ShowApplyFilterWin();
        }

        
        private void MovieDetailsGrid_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                MovieDetailsGrid_CellDoubleClick(sender, null);
                e.Handled = true;
            }
            
        }

        private void clearFilter_Click(object sender, EventArgs e)
        {
            clearFilter.Hide();
            HelperDataProvider.getDataStore().addFilter(null);
            WinDisplayControllor.DisposeApplyFilterWin();
            WinDisplayControllor.ApplyChangesOnLoadedWin();
            
        }

        private void MovieDetailsGrid_MouseClick(object sender, MouseEventArgs e)
        {
            
            if (e.Button == MouseButtons.Right)
            {
                movieListContextMenu.Show(MovieDetailsGrid,e.X, e.Y);
            }
            else
            {
                movieListContextMenu.Hide();
            }
        }

        private void movieListContextMenu_Opening(object sender, CancelEventArgs e)
        {

        }

        private void makeACopyToToolStripMenuItem_Click(object sender, EventArgs e)
        {
            showCopyMoveDeleteWin(0);
        }

        private void moveCurrentToToolStripMenuItem_Click(object sender, EventArgs e)
        {
            showCopyMoveDeleteWin(1);
        }

        private void deleteFolderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            showCopyMoveDeleteWin(2);
        }

        private void showCopyMoveDeleteWin(int action)
        {
            movieListContextMenu.Hide();
            DataGridViewSelectedRowCollection collection = MovieDetailsGrid.SelectedRows;
            List<String[]> sourceDetails = new List<string[]>();
            foreach (DataGridViewRow rows in collection)
            {
                MovieDetails details = (MovieDetails)rows.Tag;
                String[] data = new String[2];
                data[0] = details.Name;
                data[1] = details.LoadLocation;
                sourceDetails.Add(data);
            }
            FormMovieFileAction form = new FormMovieFileAction(sourceDetails, action);
            form.ShowDialog();
        }

        private void MovieDetailsGrid_SelectionChanged(object sender, EventArgs e)
        {
            DataGridViewSelectedCellCollection collection =  MovieDetailsGrid.SelectedCells;
            foreach (DataGridViewCell cell in collection)
            {
                cell.OwningRow.Selected = true;
            }

        }

        delegate void InitMovieLoad();
        private void initiateMovieLoadFromThread()
        {
            if (progressBar.InvokeRequired)
            {
                try
                {
                    InitMovieLoad d = new InitMovieLoad(initiateMovieLoadFromThread);
                    this.Invoke(d, new object[] { });
                }catch(Exception ex){
                    HelperContentProvider.OmitWarning(ex);
                }
            }
            else
            {
                initiateMovieLoad();
            }
        }

        
        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            initiateMovieLoadFromThread();
        }

        private void updateDetailsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            movieListContextMenu.Hide();
            DataGridViewSelectedRowCollection collection = MovieDetailsGrid.SelectedRows;
            MovieDetails details = null;
            foreach (DataGridViewRow rows in collection)
            {
                details = (MovieDetails)rows.Tag;
                break;
            }
            FormSelectOrAddMovie form = new FormSelectOrAddMovie(details, details.LoadLocation,true);
            form.ShowDialog();
        }

        private void moveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MovieDetailsGrid.FirstDisplayedScrollingRowIndex > 0)
            {
                int index = MovieDetailsGrid.FirstDisplayedScrollingRowIndex - 1;
                MovieDetailsGrid.FirstDisplayedScrollingRowIndex = index;
                //MovieDetailsGrid.Rows.SharedRow(index).Selected = true;
            }

        }

        private void moveDownToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MovieDetailsGrid.FirstDisplayedScrollingRowIndex < (MovieDetailsGrid.RowCount - 1))
            {
                int index = MovieDetailsGrid.FirstDisplayedScrollingRowIndex + 1;
                MovieDetailsGrid.FirstDisplayedScrollingRowIndex = index;
                
                //MovieDetailsGrid.Rows.SharedRow(index).Selected = true;
            }
        }

        private void moveEndToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int index = MovieDetailsGrid.RowCount - 1;
            MovieDetailsGrid.FirstDisplayedScrollingRowIndex = index;
            //MovieDetailsGrid.Rows.SharedRow(index).Selected = true;

        }

        private void moveHomeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MovieDetailsGrid.RowCount > 0)
            {
                MovieDetailsGrid.FirstDisplayedScrollingRowIndex = 0;
                //MovieDetailsGrid.Rows.SharedRow(0).Selected = true;
            }
        }

        private void keyNavigation_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

    }
}
